const mongoose = require('mongoose')
const { Schema } = mongoose
/**
 * 关于我们信息模型
 * @typedef {Object} AboutUs
 * @property {string} companyName - 公司名称
 * @property {string} contactName - 联系人姓名
 * @property {string} contactPhone - 联系电话
 * @property {string} website - 公司网站
 * @property {string} companyAddress - 公司地址
 * @property {Date} createdAt - 创建时间
 */
const AboutUsSchema = new Schema(
  {
    companyName: {
      type: String,
      required: true,
      maxlength: 100
    },
    contactName: {
      type: String,
      required: true,
      maxlength: 50
    },
    contactPhone: {
      type: String,
      required: true,
      validate: {
        validator: function (v) {
          return /^1[3-9]\d{9}$/.test(v)
        },
        message: props => `${props.value} 不是有效的手机号码!`
      }
    },
    website: {
      type: String,
      required: true,
      validate: {
        validator: function (v) {
          return /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.test(v)
        },
        message: props => `${props.value} 不是有效的网址!`
      }
    },
    companyAddress: {
      type: String,
      required: true,
      maxlength: 200
    },
    coordinates: {
      type: [Number],
      required: true,
      validate: {
        validator: function (v) {
          return v.length === 2 && v[0] >= -180 && v[0] <= 180 && v[1] >= -90 && v[1] <= 90
        },
        message: '无效的经纬度坐标'
      }
    },
    createdAt: {
      type: Date,
      default: Date.now
    }
  },
  { timestamps: true }
)

// 创建2dsphere索引
AboutUsSchema.index({ coordinates: '2dsphere' })

module.exports = mongoose.model('AboutUs', AboutUsSchema)
